﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CompanyManagementDLL;
using CompanyManagementDLL.DLL;
using CompanyManagementDLL.MetaData;
using log4net;

namespace CompanyManagement.Controllers
{
    public class UserController : Controller
    {
        //
        // GET: /User/

        UserDLL dllUser = new UserDLL();
        private static readonly ILog Log = LogManager.GetLogger(typeof(UserController));

        #region Page View

        public ActionResult Index()
        {
            if (!Utility.checkCookie())
            {
                return RedirectToAction("Index", "Login");
            }
            Utility.setCookiValue();
            List<UserMetaData> userlist = new List<UserMetaData>();
            try
            {
                int userid = Utility.getUserID();
                userlist = dllUser.GetUserList().Where(u => u.UserID != userid).ToList();
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return View(userlist);
        }

        public ActionResult Create()
        {
            if (!Utility.checkCookie())
            {
                return RedirectToAction("Index", "Login");
            }
            return View();
        }

        public ActionResult Edit(int id)
        {
            if (!Utility.checkCookie())
            {
                return RedirectToAction("Index", "Login");
            }
            UserMetaData UserMetaData = new UserMetaData();
            try
            {
                UserMetaData = dllUser.GetUserByID(id);
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return View(UserMetaData);
        }

        #endregion

        #region DML Operation

        [HttpPost]
        public ActionResult Create(UserMetaData usermetadta)
        {
            UserMetaData UserMetaData = new UserMetaData();
            try
            {
                if (ModelState.IsValid)
                {
                    dllUser.InsertUser(usermetadta);
                }
                else
                {
                    return View(usermetadta);
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
                return View(usermetadta);
            }
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult Edit(UserMetaData usermetadta)
        {
            UserMetaData UserMetaData = new UserMetaData();
            try
            {
                if (ModelState.IsValid)
                {
                    dllUser.UpdateUser(usermetadta);
                }
                else
                {
                    return View(usermetadta);
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
                return View(usermetadta);
            }
            return RedirectToAction("Index");
        }

        public ActionResult Delete(int id)
        {
            try
            {
                dllUser.DeleteUser(id);
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return RedirectToAction("Index");
        }
        #endregion

    }
}
